<template>
  <div class="cinema-list-page">
    <!-- 影院列表 -->
    <section class="cinemas-container">
      <div class="cinemas-header">
        <h2>影院列表</h2>
        <button class="sort-btn">默认排序</button>
      </div>

      <div class="cinema-card" v-for="cinema in cinemaList" :key="cinema.cinemaId">
        <div class="cinema-info">
          <!-- 影院名称跳转详情页 -->
          <router-link :to="`/cinemaDetail/${cinema.cinemaId}`" class="cinema-name-link">
            <h3 class="cinema-name">{{ cinema.cinemaName }}</h3>
          </router-link>
          <p class="cinema-address">地理位置：{{ cinema.cinemaAddress }}</p>
          <div class="cinema-tags">影院服务：
            <span v-for="(service, index) in splitServices(cinema.services)" :key="index" class="tag">
              {{ service }}
            </span>
          </div>
          <div class="contact-info">
            <span class="phone">联系电话：{{ cinema.phone }}</span>
          </div>
        </div>
        <div class="cinema-actions">
          <div class="price-info">
            <span class="price">¥{{ cinema.min_price || 30 }}起</span>
            <span class="distance">{{ cinema.distance || '3.2km' }}</span>
          </div>
          <!-- 选座购票按钮使用router-link跳转 -->
          <router-link :to="`/cinemaDetail/${cinema.cinemaId}`"
            class="book-btn"
          >
            选座购票
          </router-link>
        </div>
      </div>
    </section>

    <!-- 分页 -->
    <div class="pagination">
      <a href="#" v-if="currentPage !== 1" @click.prevent="getCinemaList(currentPage - 1)">上一页</a>
      <span class="active">{{ currentPage }}</span>
      <a href="#" v-if="currentPage !== pages" @click.prevent="getCinemaList(currentPage + 1)">下一页</a>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { getCinemaByPage } from "../api/cinema.js";

// 影院列表数据相关
const cinemaList = ref([]);  // 影院数组
const pages = ref(0);        // 总页数
const currentPage = ref(1);  // 当前页码
const total = ref(0);        // 总记录数

// 页面加载时获取第一页数据
onMounted(() => {
  getCinemaList(1);
});

// 获取影院列表数据
async function getCinemaList(pageNum) {
  console.log("请求页码：" + pageNum);
  
  try {
    // 调用API获取数据
    const response = await getCinemaByPage(pageNum);
    console.log("后端响应数据：", response);

    // 解析响应数据（根据实际后端返回结构调整）
    if (response.data && response.data.data) {
      cinemaList.value = response.data.data.records;
      pages.value = response.data.data.pages;
      currentPage.value = response.data.data.current;
      total.value = response.data.data.total;
      
      console.log("解析后的影院列表：", cinemaList.value);
      console.log("总页数：", pages.value);
      console.log("当前页：", currentPage.value);
      console.log("总记录数：", total.value);
    } else {
      console.error("响应数据格式不符合预期");
    }
  } catch (error) {
    console.error("获取影院数据失败：", error);
  }
}

// 拆分服务标签（假设后端services是逗号分隔的字符串）
function splitServices(services) {
  if (!services) return [];
  return services.split(',');
}
</script>

<style scoped>
/* 样式保持不变，仅新增router-link按钮样式兼容 */
.cinema-list-page {
  width: 1200px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: #f5f5f5;
  position: relative;
}

/* 影院列表样式 */
.cinemas-container {
  width: 1200px;
  flex: 1;
  padding: 20px;
  margin-right: 200px;
  box-sizing: border-box;
}

.cinemas-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.cinemas-header h2 {
  font-size: 18px;
  color: #333;
}

.sort-btn {
  background: none;
  border: 1px solid #ddd;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.cinema-card {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 15px;
  margin-bottom: 10px;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.cinema-info {
  flex: 1;
}

.cinema-name-link {
  text-decoration: none;
}

.cinema-name {
  font-size: 16px;
  font-weight: bold;
  color:  #A2131D;
  margin-bottom: 8px;
  font-weight: 500;
  transition: color 0.2s;
}

.cinema-name:hover {
  color: #A2131D;
}

.cinema-address {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
  line-height: 1.5;
}

.contact-info {
  margin-top: 8px;
  font-size: 14px;
  color: #666;
}

.cinema-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 8px;
}

.tag {
  display: inline-block;
  padding: 2px 6px;
  font-size: 12px;
  color: #A2131D;
  border: 1px solid #A2131D;
  border-radius: 3px;
}

.cinema-actions {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: flex-end;
  width: 150px;
}

.price-info {
  text-align: right;
}

.price {
  color: #A2131D;
  font-weight: bold;
  font-size: 16px;
}

.distance {
  display: block;
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

/* 选座购票按钮（router-link）样式 */
.book-btn {
  background-color: #A2131D;
  color: white;
  border: none;
  padding: 8px 15px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
  text-decoration: none; /* 去除链接下划线 */
  display: inline-block; /* 确保padding生效 */
}

.book-btn:hover {
  background-color: #8a1019;
  color: white; /*  hover时保持文字颜色 */
}

/* 分页样式 */
.pagination {
  margin: 30px 0;
  text-align: center;
  padding-right: 200px;
}

.pagination a {
  display: inline-block;
  padding: 6px 12px;
  margin: 0 5px;
  border: 1px solid #ddd;
  text-decoration: none;
  color: #333;
}

.pagination .active {
  display: inline-block;
  padding: 6px 12px;
  margin: 0 5px;
  background: #A2131D;
  color: white;
  border: 1px solid #A2131D;
}

@media (max-width: 768px) {
  .cinema-list-page {
    width: 100%;
  }
  
  .cinemas-container {
    width: 100%;
    margin-right: 0;
    padding: 10px;
  }
  
  .cinema-card {
    flex-direction: column;
  }
  
  .cinema-actions {
    width: 100%;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
  }
  
  .price-info {
    text-align: left;
  }
  
  .pagination {
    padding-right: 0;
  }
}
</style>